package 多线程.线程池;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class MyThreadPool {
    private BlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(1000);

    //初始化线程池
    public MyThreadPool(int n){
        //创建若干线程池
        for(int i = 0; i < n; i++){
            Thread t = new Thread(() -> {
                    try {
                        while(true){
                            Runnable runnable = queue.take();
                            runnable.run();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

            });
            t.start();
        }
    }
    //把任务添加到线程池
    void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
}
class Main1{
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThread = new MyThreadPool(10);
        for(int i = 0;i < 1000;i++){
            int id = i;
            myThread.submit(new Runnable()  {
                @Override
                public void run(){
                    System.out.println("hello :"+id+" " + Thread.currentThread().getName());
                }
            });
        }
    }
}